package com.shlh.saas.service.customs.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shlh.saas.entity.customs.CustomTrade;
import com.shlh.saas.mapper.customs.CustomTradeMapper;
import com.shlh.saas.service.customs.CustomTradeService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 海关贸易数据服务实现类
 */
@Service
public class CustomTradeServiceImpl extends ServiceImpl<CustomTradeMapper, CustomTrade> implements CustomTradeService {
    
    /**
     * 根据产品关键词查询海关贸易数据
     * @param productKeyword 产品关键词
     * @return 海关贸易数据列表
     */
    @Override
    public List<CustomTrade> findByProductKeyword(String productKeyword) {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(CustomTrade::getProductKeywordEnglish, productKeyword);
        return list(wrapper);
    }
    
    /**
     * 根据海关编码查询海关贸易数据
     * @param hsCode 海关编码
     * @return 海关贸易数据列表
     */
    @Override
    public List<CustomTrade> findByHsCode(String hsCode) {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(CustomTrade::getHsCode, hsCode);
        return list(wrapper);
    }
    
    /**
     * 根据提单号查询海关贸易数据
     * @param billOfLadingNumber 提单号
     * @return 海关贸易数据
     */
    @Override
    public CustomTrade findByBillOfLadingNumber(String billOfLadingNumber) {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomTrade::getBillOfLadingNumber, billOfLadingNumber);
        return getOne(wrapper);
    }
    
    /**
     * 根据供应商查询海关贸易数据
     * @param supplier 供应商
     * @return 海关贸易数据列表
     */
    @Override
    public List<CustomTrade> findBySupplier(String supplier) {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(CustomTrade::getSupplierEnglish, supplier);
        return list(wrapper);
    }
    
    /**
     * 根据采购商查询海关贸易数据
     * @param purchaser 采购商
     * @return 海关贸易数据列表
     */
    @Override
    public List<CustomTrade> findByPurchaser(String purchaser) {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(CustomTrade::getPurchaserEnglish, purchaser);
        return list(wrapper);
    }
    
    /**
     * 根据多条件分页查询海关贸易数据
     * @param productKeyword 产品关键词
     * @param hsCode 海关编码
     * @param originCountry 原产国
     * @param destinationCountry 目的国
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @Override
    public Page<CustomTrade> searchByConditions(
            String productKeyword,
            String hsCode,
            String originCountry,
            String destinationCountry,
            LocalDate startDate,
            LocalDate endDate,
            int pageNum,
            int pageSize) {
        
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(productKeyword)) {
            wrapper.like(CustomTrade::getProductKeywordEnglish, productKeyword);
        }
        
        if (StringUtils.hasText(hsCode)) {
            wrapper.like(CustomTrade::getHsCode, hsCode);
        }
        
        if (StringUtils.hasText(originCountry)) {
            wrapper.like(CustomTrade::getOriginCountry, originCountry);
        }
        
        if (StringUtils.hasText(destinationCountry)) {
            wrapper.like(CustomTrade::getDestinationCountry, destinationCountry);
        }
        
        if (startDate != null) {
            wrapper.ge(CustomTrade::getStartDate, startDate);
        }
        
        if (endDate != null) {
            wrapper.le(CustomTrade::getEndDate, endDate);
        }
        
        Page<CustomTrade> page = new Page<>(pageNum, pageSize);
        return page(page, wrapper);
    }
    
    /**
     * 获取所有产品关键词
     * @return 产品关键词列表
     */
    @Override
    public List<String> getAllProductKeywords() {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CustomTrade::getProductKeywordEnglish);
        wrapper.isNotNull(CustomTrade::getProductKeywordEnglish);
        wrapper.ne(CustomTrade::getProductKeywordEnglish, "");
        
        return list(wrapper).stream()
                .map(CustomTrade::getProductKeywordEnglish)
                .distinct()
                .collect(Collectors.toList());
    }
    
    /**
     * 获取所有海关编码
     * @return 海关编码列表
     */
    @Override
    public List<String> getAllHsCodes() {
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(CustomTrade::getHsCode);
        wrapper.isNotNull(CustomTrade::getHsCode);
        wrapper.ne(CustomTrade::getHsCode, "");
        
        return list(wrapper).stream()
                .map(CustomTrade::getHsCode)
                .distinct()
                .collect(Collectors.toList());
    }
    
    /**
     * 根据条件计算符合条件的记录总数
     * @param productKeyword 产品关键词
     * @param hsCode 海关编码
     * @param originCountry 原产国
     * @param destinationCountry 目的国
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 记录总数
     */
    @Override
    public long countByConditions(
            String productKeyword,
            String hsCode,
            String originCountry,
            String destinationCountry,
            LocalDate startDate,
            LocalDate endDate) {
        
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(productKeyword)) {
            wrapper.like(CustomTrade::getProductKeywordEnglish, productKeyword);
        }
        
        if (StringUtils.hasText(hsCode)) {
            wrapper.like(CustomTrade::getHsCode, hsCode);
        }
        
        if (StringUtils.hasText(originCountry)) {
            wrapper.like(CustomTrade::getOriginCountry, originCountry);
        }
        
        if (StringUtils.hasText(destinationCountry)) {
            wrapper.like(CustomTrade::getDestinationCountry, destinationCountry);
        }
        
        if (startDate != null) {
            wrapper.ge(CustomTrade::getStartDate, startDate);
        }
        
        if (endDate != null) {
            wrapper.le(CustomTrade::getEndDate, endDate);
        }
        
        return count(wrapper);
    }
    
    /**
     * 根据条件查询海关贸易数据用于导出
     * @param productKeyword 产品关键词
     * @param hsCode 海关编码
     * @param originCountry 原产国
     * @param destinationCountry 目的国
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param limit 限制查询数量
     * @return 海关贸易数据列表
     */
    @Override
    public List<CustomTrade> findByConditionsForExport(
            String productKeyword,
            String hsCode,
            String originCountry,
            String destinationCountry,
            LocalDate startDate,
            LocalDate endDate,
            Integer limit) {
        
        LambdaQueryWrapper<CustomTrade> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(productKeyword)) {
            wrapper.like(CustomTrade::getProductKeywordEnglish, productKeyword);
        }
        
        if (StringUtils.hasText(hsCode)) {
            wrapper.like(CustomTrade::getHsCode, hsCode);
        }
        
        if (StringUtils.hasText(originCountry)) {
            wrapper.like(CustomTrade::getOriginCountry, originCountry);
        }
        
        if (StringUtils.hasText(destinationCountry)) {
            wrapper.like(CustomTrade::getDestinationCountry, destinationCountry);
        }
        
        if (startDate != null) {
            wrapper.ge(CustomTrade::getStartDate, startDate);
        }
        
        if (endDate != null) {
            wrapper.le(CustomTrade::getEndDate, endDate);
        }
        
        // 添加限制
        if (limit != null && limit > 0) {
            Page<CustomTrade> page = new Page<>(1, limit);
            Page<CustomTrade> resultPage = page(page, wrapper);
            return resultPage.getRecords();
        } else {
            return list(wrapper);
        }
    }
} 